/*
 *
 gcc TextureWrap.c  -L. -lGLESv2 -lmingw32 -lSDL2_test -lSDL2main -lSDL2 -lSDL2_ttf && a 
 gcc TextureWrap.c  -lSDL2_test -lSDL2 -lSDL2_ttf -lm && ./a.out
 xp good, android may not.
 */
#include "SDL2/SDL.h"
#include "gles2base.h"
int Init ( UserData*userData)
{
	GLbyte vShaderStr[] =
		"uniform float u_offset;      \n"
		"attribute vec4 a_position;   \n"
		"attribute vec2 a_texCoord;   \n"
		"varying vec2 v_texCoord;     \n"
		"void main()                  \n"
		"{                            \n"
		"   gl_Position = a_position; \n"
		"   gl_Position.x += u_offset;\n"
		"   v_texCoord = a_texCoord;  \n"
		"}                            \n";

	GLbyte fShaderStr[] =  
		"precision mediump float;                            \n"
		"varying vec2 v_texCoord;                            \n"
		"uniform sampler2D s_texture;                        \n"
		"void main()                                         \n"
		"{                                                   \n"
		"  gl_FragColor = texture2D( s_texture, v_texCoord );\n"
		"}                                                   \n";
	// Load the shaders and get a linked program object
	userData->programObject = esLoadProgram ( vShaderStr, fShaderStr );
	// Get the attribute locations
	userData->positionLoc = ctx.glGetAttribLocation ( userData->programObject, "a_position" );
	userData->texCoordLoc = ctx.glGetAttribLocation ( userData->programObject, "a_texCoord" );
	// Get the sampler location
	userData->samplerLoc = ctx.glGetUniformLocation ( userData->programObject, "s_texture" );
	// Get the offset location
	userData->offsetLoc = ctx.glGetUniformLocation( userData->programObject, "u_offset" );
	// Load the texture
	SDL_Surface *surface = SDL_LoadBMP("icon.bmp");
	surface = NULL;
	SDL_Color black = { 0xff, 0x00, 0x00, 0 };
	surface = get_font(10,"hi",&black);
	if ( ! surface ) {
		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to load icon.bmp: %s\n", SDL_GetError());
		/*userData->textureId = CreateTexture2D ();*/
		userData->textureId = CreateSimpleTexture2D ();
	}else{
		GLfloat texcoords[4];
		userData->textureId = SDL_GL_LoadTexture(surface, texcoords);
		SDL_FreeSurface(surface);
	}
	ctx.glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f );
	return 1;
}
void Draw ( UserData*userData)
{
	GLfloat vVertices[] = { -0.3f,  0.3f, 0.0f, 1.0f,  // Position 0
		-1.0f,  -1.0f,              // TexCoord 0 
		-0.3f, -0.3f, 0.0f, 1.0f, // Position 1
		-1.0f,  2.0f,              // TexCoord 1
		0.3f, -0.3f, 0.0f, 1.0f, // Position 2
		2.0f,  2.0f,              // TexCoord 2
		0.3f,  0.3f, 0.0f, 1.0f,  // Position 3
		2.0f,  -1.0f               // TexCoord 3
	};
	GLushort indices[] = { 0, 1, 2, 0, 2, 3 };
	// Set the viewport
	ctx.glViewport ( 0, 0, state->window_w, state->window_h);
	// Clear the color buffer
	ctx.glClear ( GL_COLOR_BUFFER_BIT );
	// Use the program object
	ctx.glUseProgram ( userData->programObject );
	// Load the vertex position
	ctx.glVertexAttribPointer ( userData->positionLoc, 4, GL_FLOAT, 
			GL_FALSE, 6 * sizeof(GLfloat), vVertices );
	// Load the texture coordinate
	ctx.glVertexAttribPointer ( userData->texCoordLoc, 2, GL_FLOAT,
			GL_FALSE, 6 * sizeof(GLfloat), &vVertices[4] );
	ctx.glEnableVertexAttribArray ( userData->positionLoc );
	ctx.glEnableVertexAttribArray ( userData->texCoordLoc );
	// Bind the texture
	ctx.glActiveTexture ( GL_TEXTURE0 );
	ctx.glBindTexture ( GL_TEXTURE_2D, userData->textureId );
	// Set the sampler texture unit to 0
	ctx.glUniform1i ( userData->samplerLoc, 0 );
//#define GL_TRIANGLES GL_LINE_LOOP
	// Draw quad with repeat wrap mode
	ctx.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
	ctx.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
	ctx.glUniform1f ( userData->offsetLoc, -0.7f );   
	ctx.glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
	// Draw quad with clamp to edge wrap mode
	ctx.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
	ctx.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
	ctx.glUniform1f ( userData->offsetLoc, 0.0f );
	ctx.glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
	// Draw quad with mirrored repeat
	ctx.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_MIRRORED_REPEAT );
	ctx.glTexParameteri ( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_MIRRORED_REPEAT );
	ctx.glUniform1f ( userData->offsetLoc, 0.7f );
	ctx.glDrawElements ( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, indices );
	/*eglSwapBuffers ( esContext->eglDisplay, esContext->eglSurface );*/
}
